// PollutionAnalysisServiceImpl.java
package com.chinasoft.pollution_analysis.service.impl;

import com.chinasoft.pollution_analysis.entity.*;
import com.chinasoft.pollution_analysis.mapper.PollutionAnalysisMapper;
import com.chinasoft.pollution_analysis.service.PollutionAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PollutionAnalysisServiceImpl implements PollutionAnalysisService {

    @Autowired
    private PollutionAnalysisMapper pollutionAnalysisMapper;

    @Override
    public List<PollutionStats> getNationalStats() {
        return pollutionAnalysisMapper.selectNationalAvg();
    }

    @Override
    public List<RegionalStats> getRegionalStats(String city) {
        if (city != null && !city.trim().isEmpty()) {
            return pollutionAnalysisMapper.selectRegionalByCity(city);
        }
        return pollutionAnalysisMapper.selectRegionalAll();
    }

    @Override
    public List<TimeTrend> getTimeTrends(String timeUnit, String pollutantType) {
        if ("hour".equals(timeUnit)) {
            if (pollutantType != null && !pollutantType.trim().isEmpty()) {
                // 这里需要根据实际表结构调整
                return pollutionAnalysisMapper.selectHourlyTrendByPollutant(pollutantType);
            }
            return pollutionAnalysisMapper.selectHourlyTrendAll();
        } else {
            if (pollutantType != null && !pollutantType.trim().isEmpty()) {
                return pollutionAnalysisMapper.selectDailyTrendByPollutant(pollutantType);
            }
            return pollutionAnalysisMapper.selectDailyTrendAll();
        }
    }

    @Override
    public List<CityRanking> getCityRankings(String pollutantType, Integer limit) {
        if (limit == null) limit = 50;
        if (pollutantType != null && !pollutantType.trim().isEmpty()) {
            return pollutionAnalysisMapper.selectCityRankingByPollutant(pollutantType, limit);
        }
        return pollutionAnalysisMapper.selectCityRankingAll(limit);
    }

    @Override
    public List<RegionalStats> getKeyCityAnalysis() {
        return pollutionAnalysisMapper.selectKeyCityAnalysis();
    }

    @Override
    public List<PollutantDistribution> getPollutantDistribution() {
        return pollutionAnalysisMapper.selectPollutantDistribution();
    }

    @Override
    public List<HotspotAnalysis> getHotspotAnalysis(String date, String region) {
        if (date != null && region != null) {
            return pollutionAnalysisMapper.selectHotspotByDateAndRegion(date, region);
        }
        return pollutionAnalysisMapper.selectHotspotAll();
    }

    @Override
    public List<SpatioTemporalTrend> getSpatioTemporalTrend(String region, String season) {
        if (region != null && season != null) {
            return pollutionAnalysisMapper.selectSpatioTemporalTrend(region, season);
        }
        return pollutionAnalysisMapper.selectSpatioTemporalTrendAll();
    }

    @Override
    public Map<String, Object> getRealTimeStats() {
        Map<String, Object> stats = new HashMap<>();

        // 添加空值检查
        Integer citiesCount = pollutionAnalysisMapper.selectCitiesCount();
        Integer pollutantsCount = pollutionAnalysisMapper.selectPollutantsCount();
        Long totalRecords = pollutionAnalysisMapper.selectTotalRecords();

        stats.put("citiesCount", citiesCount != null ? citiesCount : 0);
        stats.put("pollutantsCount", pollutantsCount != null ? pollutantsCount : 0);
        stats.put("totalRecords", totalRecords != null ? totalRecords : 0);
        stats.put("lastUpdate", System.currentTimeMillis());

        return stats;
    }

    @Override
    public List<String> getPollutantTypes() {
        return pollutionAnalysisMapper.selectPollutantTypes();
    }

    @Override
    public List<String> getCityList() {
        return pollutionAnalysisMapper.selectCityList();
    }
}